package com.aigou.admin.service.impl;

import com.aigou.admin.utils.Md5Util;
import com.aigou.admin.dao.UserDao;
import com.aigou.admin.entity.PageEntity;
import com.aigou.admin.entity.User;
import com.aigou.admin.service.UserService;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Author: Hor
 * @Date: 2024/11/29 09:30
 * @Version: 1.0
 */

@Service
@AllArgsConstructor
public class UserServiceImpl implements UserService {

    private final UserDao userDao;


    @Override
    public List<User> findAll() {
        return userDao.selectAll();
    }

    @Override
    public User findById(String id) {
        return userDao.selectById(id);
    }

    @Override
    public Boolean save(User user) {
        return userDao.insert(user);
    }

    @Override
    public Boolean update(User user) {
        return userDao.update(user);
    }

    @Override
    public Boolean remove(String id) {
        return userDao.delete(id);
    }

    @Override
    public User findByUsername(String username) {
        return userDao.selectByUsername(username);
    }

    @Override
    public User login(String username, String password) {
        User user = findByUsername(username);
        if (Md5Util.checkPassword(password, user.getUser_pwd())) {
            return user;
        }
        return null;
    }

    /**
     * 分页查询用户
     *
     * @param currentPage 当前页
     * @param pageSize    每页数量
     * @return PageEntity<User>
     */
    @Override
    public PageEntity<User> findUsersByPage(Integer currentPage, Integer pageSize,String username) {
        Integer total = userDao.selectCount(username);
        List<User> users = userDao.selectUserByLimit((currentPage - 1) * pageSize, pageSize,username);
        PageEntity<User> pageEntity = new PageEntity<>();
        pageEntity.setTotal(total);
        pageEntity.setPageSize(pageSize);
        pageEntity.setCurrent(currentPage);
        pageEntity.setRecords(users);
        return pageEntity;
    }
}
